<?php

namespace App\Modules\Apicrzs\Services\Pay;

use App\Library\Junda\src\CardService;
use App\Library\Junda\src\Consumption;
use App\Library\Junda\src\EquipmentService;
use App\Library\Wechat\PayWeChatApi;
use App\Modules\Admin\Entities\Equipment\EquipmentParamsFreeModel;
use App\Modules\Admin\Entities\Member\UserCardModel;
use App\Modules\Admin\Entities\Water\WaterOrderModel;
use App\Modules\Apicrzs\Services\BaseServices;
use App\Modules\Apicrzs\Services\Water\WaterOrderService;
use app\services\pay\PayServices;

class PayWaterOrderServices extends BaseServices
{
    public   function getOrderInfo()
    {
        // 昨天
        $day_time = date("Y-m-d", strtotime("-1 day"));
        $start_time = strtotime($day_time . ' 00:00:00');
        $end_time = strtotime($day_time . ' 23:59:59');
        $lists =  WaterOrderModel::getInstance()->whereBetween( 'created_time', [$start_time, $end_time])
            ->where(['is_water'=>0])->orderBy('created_time','desc')->get()->toArray();
        $order_list=[];
        foreach ($lists as $item){
            $order_sn = $item['order_sn'];
            $item_wx = PayWeChatApi::WXPayNotify($order_sn);
            if ($item_wx['http_code']==0) {
                $result_wx = $item_wx['data'];

                if ($result_wx['trade_state']=='SUCCESS') {
                    WaterOrderModel::getInstance()->where(['order_id' => $item['order_id']])->update([
                        'trade_no' => $result_wx['transaction_id'],
                        'status' => 1,
                    ]);
                    $res_junda = Consumption::GetPayConsumeByOrderNo($order_sn);

                    if ($res_junda['success']){
                        WaterOrderModel::getInstance()->where(['order_id' => $item['order_id']])->update([
                            'trade_no' => $result_wx['transaction_id'],
                            'status' => 1,
                            'is_water' => 1,
                        ]);
                    }else {
                        $order_list[] = [
                            'order_sn' => $item['order_sn'],
                            'buy_money' => $item['buy_money'],
                            'amount' => $item['amount'],
                            'pay_type' => $item['pay_type'],
                            'created_time' => $item['created_time'],
                            'pay_item' => $item_wx ?? [],
                            'junda_item' => $res_junda ?? [],
                        ];
                    }
                }elseif ($result_wx['trade_state']=='NOTPAY'){
                    WaterOrderModel::getInstance()->where(['order_id' => $item['order_id']])->update([
                        'status' => -1,
                        'is_water' => -1,
                    ]);
                }
            }
        }

        if ($item_wx['trade_state']=='SUCCESS'){
            //
            $transaction_id = $item_wx['transaction_id'];

            exit(json_encode($res_junda));
        }elseif ($item_wx['trade_state']=='REFUND'){
            //订单发生过退款，退款详情请查询退款单
        }


        foreach ($lists as $item){


        }
    }
}
